package cn.jbolt.core.bean;

import cn.hutool.core.util.NumberUtil;
import cn.jbolt.core.util.JBoltArrayUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.jfinal.kit.StrKit;

import java.math.BigDecimal;

/**
 * rangeSlider值
 */
public class RangeSlider {
    public static final String SPLIT = ";";
    private String from;
    private String to;

    private RangeSlider(String from, String to) {
        this.from = from;
        this.to = to;
    }

    public static RangeSlider create(String from, String to) {
        return new RangeSlider(from, to);
    }

    public static RangeSlider fromSplit(String values, String split) {
        if (StrKit.isBlank(values)) {
            return null;
        }
        if(StrKit.isBlank(split)){split = SPLIT;}
        if (values.contains(split)) {
            String[] nums = JBoltArrayUtil.from(values,split);
            if (nums.length != 2 || !NumberUtil.isNumber(nums[0]) || !NumberUtil.isNumber(nums[1])) {
                throw new RuntimeException("rangeSlider 参数必须为数字 或者 数字"+SPLIT+"数字 格式");
            }
            return create(nums[0],nums[1]);
        }
        if (!NumberUtil.isNumber(values)) {
            throw new RuntimeException("rangeSlider 参数必须为数字 或者 数字"+SPLIT+"数字 格式");
        }
        return create(values,values);
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }
    @JSONField(serialize = false)
    public int getFromIntValue() {
        return Integer.parseInt(from);
    }
    @JSONField(serialize = false)
    public int getToIntValue() {
        return Integer.parseInt(to);
    }
    @JSONField(serialize = false)
    public double getFromDoubleValue() {
        return Double.parseDouble(from);
    }
    @JSONField(serialize = false)
    public double getToDoubleValue() {
        return Double.parseDouble(to);
    }
    @JSONField(serialize = false)
    public BigDecimal getFromBigDecimalValue() {
        return new BigDecimal(from);
    }
    @JSONField(serialize = false)
    public BigDecimal getToBigDecimalValue() {
        return new BigDecimal(to);
    }

}
